package com.example.administrator.szb.fragments.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.webviews.WebViewsActivity;
import com.example.administrator.szb.adapter.Anliadapter;
import com.example.administrator.szb.bean.AnliBean;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.http.URLAddress;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/1/12.
 */

public class SearchANli extends MVPBaseFragment {
    View ll_nodata;
    ListView recyclerView;
    Anliadapter adapter;//回答
    private TextView top_tips;
    ArrayList<AnliBean> mdata_referListBean = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_searchs2,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        top_tips = view.findViewById(R.id.top_tips);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setDivider(null);
        recyclerView.setDividerHeight(0);
        ll_nodata = view.findViewById(R.id.ll_nodata);
        isLoaded = true;
        recyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, WebViewsActivity.class);
                intent.putExtra("url", URLAddress.ANLI_URL+((AnliBean)mdata_referListBean.get(position)).getId());
                intent.putExtra("bar_title","案例详情");
                intent.putExtra("show",true);
                intent.putExtra("type",3);
                intent.putExtra("titles",((AnliBean)mdata_referListBean.get(position)).getTitle());
                intent.putExtra("contents",((AnliBean)mdata_referListBean.get(position)).getContent());
                intent.putExtra("id",((AnliBean)mdata_referListBean.get(position)).getId());
                startActivity(intent);
            }
        });
    }
    private boolean isLoaded = false;
    private boolean isfirst = true;
    @Override
    protected void initEvent() {
        if (!isLoaded){
            return;
        }
        if (mdata_referListBean.size()==0){
            if (!isfirst){
                recyclerView.setVisibility(View.GONE);
                ll_nodata.setVisibility(View.VISIBLE);
            }
        }else {
            ll_nodata.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        top_tips.setVisibility(View.VISIBLE);
        top_tips.setText("共搜索到"+mdata_referListBean.size()+"条相关案例学习");
        if (adapter==null){
            adapter= new Anliadapter(context,mdata_referListBean);
            recyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        isfirst = false;
    }

    @Override
    protected void initData() {

    }
    public void setDatas(List list){
        if (list==null){
            list = new ArrayList();
        }
        mdata_referListBean.clear();
        mdata_referListBean.addAll(list);
        initEvent();
    }


}
